import { Component } from '@angular/core';

import { AlertController, NavController, NavParams } from 'ionic-angular';


import { Storage } from '@ionic/storage';

import _ from 'lodash';
import {MarketingLineupSaveService} from "./save.service";

@Component({
  selector: 'page-marketing-lineup-save',
  templateUrl: './save.html',
  providers: [MarketingLineupSaveService]
})
export class MarketingLineupSavePage {
	moa_page_name: any = "page-marketing-lineup-save";
  moa_page_explain: any = "";

  defaultEmpByTree = [];

  saveEntity = null;

  maxDate = "";

  isEditData = false;

  title = "";

  detail = null;

  taskIsFinish = false;

  constructor(
    public alertCtrl: AlertController,
    public navCtrl: NavController,
    private navParams: NavParams,
    public storage: Storage,
    public saveService: MarketingLineupSaveService
  ) {
    this.title = "新增任务";
    let isEdit = this.navParams.get("isEdit");
    let detail = this.navParams.get("detail");
    this.initSaveEnity();
    if (isEdit) {
      this.title = "编辑任务";
      this.isEditData = true;
      this.detail = detail;
      this.setSaveEntity(detail);
    }

    this.moa_page_explain = "任务管理 - " + this.title;
  }

  /**
   * 初始化保存实例对象
   */
  initSaveEnity() {
    let that = this;
    this.saveEntity = {
      edittype_i: 0,
      l_usr_id_i: "",
      l_task_id_i: "",
      vc_task_name_i: "",
      vc_task_content_i: "",
      l_urgent_type_i: "2",
      c_is_done_i: "",
      l_task_score_i: 0
    };

    this.storage.get("userInfo").then(user => {
        this.saveService.getUsrIDFromOAServer(user.loginid).subscribe(usr_id => {
          that.saveEntity.l_usr_id_i = usr_id[0].usr_id;
        });
    });
  }

  setSaveEntity(detail) {
    console.log(detail);
    this.saveEntity.edittype_i = 1;
    // this.saveEntity.l_usr_id_i = detail.l_usr_id_i;
    this.saveEntity.l_task_id_i = detail.L_TASK_ID;
    this.saveEntity.vc_task_name_i = detail.Vc_task_name;
    this.saveEntity.vc_task_content_i = detail.vc_task_content;
    if (detail.L_urgent_type == "是") {
      this.saveEntity.l_urgent_type_i = "1";
    } else {
      this.saveEntity.l_urgent_type_i = "2";
    }
    if (detail.C_is_done == "完成") {
      this.taskIsFinish = true;
      this.saveEntity.c_is_done_i = "Y";
    } else {
      this.saveEntity.c_is_done_i = "N";
    }
    this.saveEntity.l_task_score_i = detail.l_task_score;
  }

  selectFinishChange() {
    if (this.saveEntity.c_is_done_i == "Y") {
      this.taskIsFinish = true;
      this.saveEntity.l_task_score_i = 0;
    } else {
      this.taskIsFinish = false;
    }
  }

  validateData() {
    let result = true;
    let message = "";
    if (this.saveEntity.vc_task_name_i === "" || this.saveEntity.vc_task_name_i == null) {
      console.log("validateData");
      console.log(this.saveEntity.vc_task_name_i);
      message = "请输入任务名称";
      return message;
    }
    if (this.saveEntity.vc_task_content_i === "" || this.saveEntity.vc_task_content_i == null) {
      console.log("validateData");
      console.log(this.saveEntity.vc_task_name_i);
      message = "请输入任务内容";
      return message;
    }
    if (this.taskIsFinish && (_.parseInt(this.saveEntity.l_task_score_i) > 100
      || _.parseInt(this.saveEntity.l_task_score_i) < 0)) {
      message = "打分的分数只能介于[0-100]";
      return message;
    }
    return result;
  }

  clickEnable = true;
  /**
   * 保存工作日报
   */
  save() {
    let validateResult = this.validateData();
    if (validateResult != true) {
      let alert = this.alertCtrl.create({
        title: "提示",
        message: validateResult,
        buttons: [
          {
            text: "确定",
            role: 'cancel',
            handler: () => {
            }
          }
        ]
      });
      alert.present();
      return;
    }

    if (!this.clickEnable) {
      return;
    } else {
      this.clickEnable = false;
    }

    this.saveService.saveData(this.saveEntity).subscribe(result => {
      console.log(result);

      if (result && result[0].o_errcode == 0) {
        let alert = this.alertCtrl.create({
          title: "提示",
          message: "保存成功",
          buttons: [
            {
              text: "确定",
              role: 'cancel',
              handler: () => {
                this.storage.set("doRefresh", true);
                if (this.isEditData) {
                  let detail = this.navParams.get("detail");
                  detail.L_TASK_ID = this.saveEntity.l_task_id_i;
                  detail.Vc_task_name = this.saveEntity.vc_task_name_i;
                  detail.vc_task_content = this.saveEntity.vc_task_content_i;
                  if (this.saveEntity.l_urgent_type_i == 1) {
                    detail.L_urgent_type = "是";
                  } else {
                    detail.L_urgent_type = "否";
                  }
                  if (this.saveEntity.c_is_done_i == "Y") {
                    detail.C_is_done = "完成"
                  } else {
                    detail.C_is_done = "未完成"
                  }
                  detail.l_task_score = this.saveEntity.l_task_score_i;
                }
                this.navCtrl.pop();
              }
            }
          ]
        });
        alert.present();
      } else {
        this.clickEnable = true;
      }
    });
  }

}
